//
//  XS_CourseItemCell.swift
//  XueDao
//
//  Created by fandongtongxue on 2020/12/9.
//

import UIKit

class XS_CourseItemCell: UICollectionViewCell {

    @IBOutlet weak var imgView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var freeLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    @IBOutlet weak var oldPriceLabel: UILabel!
    
    @IBOutlet weak var buyBtn: UIButton!
    var _model : XS_CourseItemModel!
    public var model : XS_CourseItemModel!{
        set{
            _model = newValue
            //设置数据
            imgView.sd_setImage(with: URL.init(string: newValue.curriculumImgUrl), completed: nil)
            titleLabel.text = newValue.curriculumName
            freeLabel.isHidden = Int(newValue.price) != 0
            priceLabel.isHidden = Int(newValue.price) == 0
            priceLabel.text = newValue.price
            contentLabel.text = newValue.purchaseNum+"人加入学习"
            
            let text = NSMutableAttributedString()
            let (truePrice, _) = xsGetPrice(vipIsFree: newValue.vipIsFree, isFree: newValue.isFree, vipPrice: newValue.vipPrice, discountPrice: newValue.discountPrice, price: newValue.price)
            if truePrice.count == 0 || truePrice == "0" {
                let attr = NSMutableAttributedString(string: "免费")
                attr.yy_font = .xsFont(name: .medium, .scale*14)
                text.append(attr)
            } else {
                do {
                    let attr = NSMutableAttributedString(string: "￥")
                    attr.yy_font = .xsFont(name: .medium, .scale*14)
                    text.append(attr)
                }
                do {
                    let attr = NSMutableAttributedString(string: "\(truePrice)")
                    attr.yy_font = .xsFont(name: .medium, .scale*14)
                    text.append(attr)
                }
            }
            text.yy_color = #colorLiteral(red: 0.9294117647, green: 0.3960784314, blue: 0.2470588235, alpha: 1)
            text.yy_alignment = .right
            priceLabel.attributedText = text
            
            if newValue.curriculumType != "live" {
                let oldText = NSMutableAttributedString(string: ("原价"+newValue.price))
                oldText.yy_strikethroughStyle = .single
                oldText.yy_strikethroughColor = #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1)
                oldText.yy_color = #colorLiteral(red: 0.6666666667, green: 0.6941176471, blue: 0.7176470588, alpha: 1)
                oldText.yy_alignment = .right
                oldPriceLabel.attributedText = oldText
            }
            
            if newValue.isBuy == "yes" {
                buyBtn.setTitle("已购买", for: .normal)
                buyBtn.backgroundColor = #colorLiteral(red: 0.1647058824, green: 0.7411764706, blue: 0.6117647059, alpha: 1)
            }else{
                buyBtn.setTitle("立即购买", for: .normal)
                buyBtn.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.3960784314, blue: 0.2470588235, alpha: 1)
            }
            buyBtn.isHidden = false
        }
        get{
            return _model
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        imgView.contentMode = .scaleAspectFill
        imgView.layer.masksToBounds = true
        imgView.layer.cornerRadius = .scale * 4
    }
    
    @IBAction func freeBtnAction(_ sender: UIButton) {
        
    }
    
    @IBAction func buyBtnAction(_ sender: UIButton) {
        
    }
}
